package com.diyizu.dao.impl;

import com.diyizu.dao.IUserDao;
import com.diyizu.domain.User;
import com.diyizu.service.impl.IRegServiceImpl;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class IUserDaoImpl implements IUserDao {

    private static Scanner sc = new Scanner(System.in);
    //将用户信息添加到磁盘中的user.txt文件中

    @Override
    public void addUser(String username, String password, String email) {
        //增
        User user = new User(username, password, email);
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        if (getUser()!=null){
            //如果有旧数据把旧数据添加到新集合中
            ArrayList<User> allStudents = getUser();
            for (User us : allStudents) {
                users.add(us);
            }
        }
        writer(users);
    }

    private void writer(ArrayList<User> addUser) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("information_system\\user.txt"));
            oos.writeObject(addUser);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    //改密码
    public void updatePass(String username, String password) {
        ArrayList<User> list = getUser();
        for (User user : list) {
            if (user.getUsername().equals(username)) {
                //1.用集合遍历user.txt，删掉旧的user
                String email = user.getUsername();
                User newUser = new User(username, password, email);
                int index = getIndex(username);
                list.set(index,newUser);
            }
        }
        //将新集合覆盖到user.txt中
        writer(list);
    }

    @Override
    //改邮箱
    public void updateEmail(String username, String email) {
        ArrayList<User> list = getUser();
        for (User user : list) {
            if (user.getUsername().equals(username)) {
                int index = getIndex(username);
                String oldPass = user.getPassword();
                User us = new User(username,oldPass,email);
                list.set(index,us);
            }
        }
        writer(list);
    }

    @Override
    //获取用户
    public ArrayList<User> getUser() {
        ObjectInputStream ois = null;
        ArrayList<User> users = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("information_system\\user.txt"));
            users = (ArrayList<User>) ois.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return users;
    }

//    //获取邮箱
    @Override
    public String getEmail(String username){
        ArrayList<User> listUser = getUser();
        for (User user : listUser) {
            if (user.getUsername().equals(username));
            return user.getEmail();
        }
        return null;
    }
    @Override
    //查看用户输入是否正确
    public boolean isUser(String username, String password) {
        ArrayList<User> list = getUser();
        for (User user : list) {
            if (user.getUsername().equals(username)){
                if (user.getPassword().equals(password)){
                    return true;
                }
            }
        }
        return false;
    }

    //获取用户在集合中的下标
    private int getIndex(String username) {
        ArrayList<User> alllist = getUser();
        for (int i = 0; i < alllist.size(); i++) {
            User us = alllist.get(i);
            if (us.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }
}
